﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mogre;

namespace WastedMechanics
{
    public class CharTaskMoveTo : CharTask
    {        
        public List<Vector3> Marshroute;
        public Vector3 Target;
        int CurrentPoint;
        public bool Run;

        public CharTaskMoveTo(Character chr, Vector3 target, bool run, string lbl = "")
            : base(chr, lbl)
        {
            Target = target;
            Run = run;
        }

        public override void Begin()
        {
            Marshroute = new List<Vector3>();
            Engine.CurrentLevel.Navmesh.SearchRoute(Character.Position, Target, Marshroute);
            CurrentPoint = 0;
        }

        public override void Update()
        {
            if (!IsFinished)
            {
                if (CurrentPoint == Marshroute.Count)
                {
                    IsFinished = true;
                    Character.State.HaltOrder();
                    return;
                }

                if (((Character.Position - Marshroute[CurrentPoint]) * new Vector3(1, 0, 1)).Length > 0.3f)
                {
                    Vector3 turnpoint = Marshroute[CurrentPoint];
                    turnpoint.y = Character.Position.y + Character.Profile.HeadOffset.y;
                    Character.TurnTo(turnpoint);
                    Character.State.MoveOrder(Run);
                }
                else
                    CurrentPoint++;                                    
            }
        }

    }
}
